<template>
  <div class="dashboard-container">
    <el-form ref="btn" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="form.password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="repassword">
        <el-input v-model="form.repassword" />
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="login">主要按钮</el-button>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'Dashboard',
  data() {
    const validator = (rule, value, callback) => {
      if (value !== this.form.password) {
        callback(new Error('两次密码不一致'))
      } else {
        callback()
      }
    }
    return {
      form: {
        username: '',
        password: '',
        mobile: '',
        repassword: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空' },
          { min: 3, max: 9, message: '用户名必须为3-9个字符' }
        ],
        password: [
          { required: true, message: '密码不能为空' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号校验不通过' }
        ],
        repassword: [
          { required: true, message: '确认密码不能为空' },
          { validator: validator }

        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  methods: {
    login() {
      this.$refs.btn.validate(isvaildate => {
        if (isvaildate) {
          console.log('发送请求')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
